package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "购物车相关")
@Slf4j
@RequestMapping("/user/shoppingCart")
public class shoppingCartController {


    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 向购物车内新增商品
     */
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result addToCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("向购物车内新增商品的请求参数为：{}",shoppingCartDTO);
        shoppingCartService.addToCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查看购物车
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> queryCart(){
        List<ShoppingCart> shoppingCartList = shoppingCartService.queryCart();
        return Result.success(shoppingCartList);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result cleanCart(){
        shoppingCartService.cleanCart();
        return Result.success();
    }

    /**
     * 删除购物车中的商品
     */
    @ApiOperation("删除购物车中的商品")
    @PostMapping("/sub")
    public Result updateShoppingCartItem(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车商品的请求参数为：{}",shoppingCartDTO);
        shoppingCartService.updateShoppingCartItem(shoppingCartDTO);
        return Result.success();
    }
}
